<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_猜数字练习</title>
</head>
<body>
<h2>已生成一个1~100的随机数</h2><br>
<input type="text" placeholder="请输入你猜的数字!" class="i1">
<button onclick="f1()">点我验证</button>
<br>
<h2>小提示:<span class="tip"></span></h2>
<script>
    let rand = parseInt(Math.random() * 100) + 1;
    let tipE = document.querySelector('.tip');
    let cnt = 0;
    function f1() {
        cnt++;
        let num = parseInt(document.querySelector('.i1').value);
        // if (isNaN(num)) {
        //     tipE.innerHTML = '请输入数字';
        //     return;
        // }
        if (!/^\d+$/.test(num)) {
            alert('请输入数字');
            return;
        }
        if (num < rand) {
            tipE.innerHTML = '猜小了';
        } else if (num > rand) {
            tipE.innerHTML = '猜大了';
        } else {
            tipE.innerHTML = '恭喜你猜对了,一共猜了' + cnt + '次';
        }
    }
</script>
</body>
</html>